package emergence.game.gfx;

public class CompositeSprite extends Sprite {
	
	private BasicSprite[][] components;
	
	private int compWidth;
	private int compHeight;
	private int gridWidth;
	private int gridHeight;
	
	public CompositeSprite(BasicSprite[][] components) {
		super();
		
		this.components = components;
		
		compWidth = components[0][0].width();
		compHeight = components[0][0].height();
		gridWidth = components.length;
		gridHeight = components[0].length;
		int width = compWidth * gridWidth;
		int height = compHeight * gridHeight;
		
		int[][] pixels = new int[width][height];
		
		for (int j = 0; j < gridHeight; j++) {
			for (int i = 0; i < gridWidth; i++) {
				BasicSprite sprite = components[i][j];
				int[][] spritePixels = sprite.pixels();
				
				for (int yPos = 0; yPos < sprite.height(); yPos++) {
					for (int xPos = 0; xPos < sprite.width(); xPos++ ) {
						pixels[xPos + i * compWidth][j * compHeight + yPos] = spritePixels[xPos][yPos];
					}
				}
			}
		}
		
		init(pixels, width, height);
	}

	
	public BasicSprite[][] getComponents() {
		return components;
	}

	
	public CompositeSprite flip(boolean flipX, boolean flipY) {
		BasicSprite[][] newComponents = new BasicSprite[gridWidth][gridHeight];
		
		if (flipX) {
//			for (int i = 0; i < gridWidth; i++) {
//				for (int j = 0; j < gridHeight; j++) {
//					
//				}
//			}
		}
		if (flipY) {
			for (int i = 0; i < gridWidth; i++) {
				for (int j = 0; j < gridHeight; j++) {
					newComponents[i][j] = (BasicSprite)components[gridWidth - i  - 1][j].flip(false, true);
				}
			}
		}
		
		return new CompositeSprite(newComponents);
	}
}
